﻿// CCamera
// © Ren Katims 2013

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace Casters.source
{
    /// <summary>
    /// Camera Class, Currently handles its own input and its only purpose is to hand a transformation matrix to the renderengine
    /// </summary>
    public class CCamera
    {
        // Variables
        public Vector2  m_vVelocity;
        public Vector2  m_vPosition;
        public Vector2  m_vViewPort;
        public float    m_fZoom;
        public bool     m_bEnabled;

        /// <summary>
        /// Checks to see if this rectangle should be culled or not
        /// </summary>
        /// <param name="_rect">Rectangle to check</param>
        /// <returns></returns>
        public bool CheckCulling(Rectangle _rect)
        {
            _rect.X = (int)((float)_rect.X * m_fZoom);
            _rect.Y = (int)((float)_rect.Y * m_fZoom);
            Rectangle rect = new Rectangle((int)m_vPosition.X, (int)m_vPosition.Y, (int)m_vViewPort.X, (int)m_vViewPort.Y);

            if(rect.Contains(_rect))
                return true;

            return false;
        }

        /// <summary>
        /// Checks to see if this point should be culled or not
        /// </summary>
        /// <param name="_pos">Point to check</param>
        /// <returns></returns>
        public bool CheckCulling(Vector2 _pos)
        {
            _pos *= m_fZoom;
            Point pos = new Point((int)_pos.X, (int)_pos.Y);
            Rectangle rect = new Rectangle((int)m_vPosition.X, (int)m_vPosition.Y, (int)m_vViewPort.X, (int)m_vViewPort.Y);

            if (rect.Contains(pos))
                return true;

            return false;
        }

        public CCamera()
        {
            m_vPosition = new Vector2(-398, -288);
            m_vVelocity = new Vector2(100, 100);
            m_vViewPort = CGame.GetInstance().viewPort;
            m_fZoom = 0.5f;
            m_bEnabled = false;
        }

        /// <summary>
        /// Creates and returns a Matrix based on stored Position, Velocity and Zoom
        /// </summary>
        public Matrix GetTransformMatrix
        {
            get 
            {
                return Matrix.CreateRotationZ(0) * Matrix.CreateTranslation(-m_vPosition.X, -m_vPosition.Y, 0);
            }
        }

        /// <summary>
        /// Update - really an input, simply takes input to adjust internal parameters
        /// </summary>
        /// <param name="_DT">Delta Time</param>
        public void Update(float _DT)
        {
            if (!m_bEnabled)
                return;

            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                m_vPosition.Y = m_vPosition.Y - (m_vVelocity.Y * _DT);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                m_vPosition.Y = m_vPosition.Y + (m_vVelocity.Y * _DT);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                m_vPosition.X = m_vPosition.X - (m_vVelocity.X * _DT);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                m_vPosition.X = m_vPosition.X + (m_vVelocity.X * _DT);
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Q))
            {
                m_fZoom = m_fZoom - (0.1f * _DT);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.E))
            {
                m_fZoom = m_fZoom + (0.1f * _DT);
            }
        }

        /*** EVENTS ***/
        // Events for all camera actions
        // Will be switched to eventually
        public void OnZoomIn(params object[] eventArgs)
        {
            if (!m_bEnabled)
                return;

            //m_fZoom = m_fZoom + (0.1f * _DT);
        }
        public void OnZoomOut(params object[] eventArgs)
        {
            if (!m_bEnabled)
                return;

           // m_fZoom = m_fZoom - (0.1f * _DT);
        }
        public void OnCameraMoveUp(params object[] eventArgs)
        {
            if (!m_bEnabled)
                return;

            // m_vPosition.Y = m_vPosition.Y + (m_vVelocity.Y * _DT);
        }
        public void OnCameraMoveDown(params object[] eventArgs)
        {
            if (!m_bEnabled)
                return;

           // m_vPosition.Y = m_vPosition.Y - (m_vVelocity.Y * _DT);
        }
        public void OnCameraMoveLeft(params object[] eventArgs)
        {
            if (!m_bEnabled)
                return;

            //m_vPosition.X = m_vPosition.X + (m_vVelocity.X * _DT);
        }
        public void OnCameraMoveRight(params object[] eventArgs)
        {
            if (!m_bEnabled)
                return;

           // m_vPosition.X = m_vPosition.X - (m_vVelocity.X * _DT);
        }
    }
}
